package top.appx.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.appx.entity.EmailEntity;
import top.appx.entity.UserEntity;
import top.appx.form.UserForm;
import top.appx.service.EmailService;
import top.appx.service.UserService;
import top.appx.util.StringUtil;
import top.appx.vo.Pager;
import top.appx.vo.ResultVO;
import top.appx.vo.UserVO;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * Created by zwy on 2015-5-20.
 */
@Controller
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private EmailService emailService;



    @RequestMapping(value= "user/controller",params ="action=queryByPage")
    @ResponseBody
    public Object queryByPage(UserEntity user,int page,int rows){
        Pager<UserVO> pager = userService.queryByPage(user,page,rows);
        return pager;
    }

    @RequestMapping(value="user/controller",params = "action=save")
    @ResponseBody
    public Object save(UserEntity user){
        userService.save(user);
        return new ResultVO();
    }

    @RequestMapping(value="user/controller",params = "action=delete")
    @ResponseBody
    public Object delete(UserEntity user){
        userService.delete(user);
        return new ResultVO();
    }
    @RequestMapping(value="user/register")
    public String register(){

        return "register";
    }
    @RequestMapping(value="email/sendEmailCheckcode")
    @ResponseBody
    public Object sendEmailCheckcode(String email,HttpSession session){
        String randomStr = StringUtil.randomCheckcode();
        session.setAttribute("checkcode", randomStr);
        session.setAttribute("email",email);
        try {
            EmailEntity e = new EmailEntity();
            e.setTitle("验证码邮件");
            e.setContent("您的验证码为:"+randomStr);
            e.setTarget(email);
            emailService.sendEmail(e);
            return ResultVO.success();
        }catch (Exception ex){
            return ResultVO.error(ex.getMessage());
        }
    }



    @RequestMapping(value="user/login")
    public String login(){
        return "login";
    }
    @RequestMapping(value="user/register",params = "method=send")
    @ResponseBody
    public Object register_send(UserForm user,String checkcode,HttpSession session){

        Object checkcode_s = session.getAttribute("checkcode");
        Object email_s = session.getAttribute("email");
        if(
                !(checkcode_s!=null&&checkcode_s.toString().equals(checkcode))
                ||
                !(email_s!=null && email_s.toString().equals(user.getEmail()))
                ){
            return ResultVO.error("邮箱验证码错误");
        }
        UserEntity user1 = new UserEntity();
        user1.setUsername(user.getUsername());
        user1.setPassword(user.getPassword());
        user1.setEmail(user.getEmail());
        user1.setSex(user.getSex());
        userService.save(user1);
        return ResultVO.success();
    }




}
